<?php

namespace addons\mental\controller;

use addons\mental\model\TestMain;
use addons\mental\model\Testlog;
use think\Cache;
use think\Db;

class Test extends Base
{
    protected $noNeedLogin = ['seachtitle', 'hottest', 'getlist', 'detail','isofficial','isLogin','comtinue','teamimage','zengtace'];
    
    
    /**
     * 团测填写资料(/addons/mental/test/teamcreate)
     * @param string $invitation_code 邀请码
     * @param string $full_name 姓名
     * @param string $phone 手机号
     * @param string $position 职位
     */
    public function teamcreate()
    {
        $invitation_code = $this->request->param('invitation_code');
        $full_name = $this->request->param('full_name');
        $phone = $this->request->param('phone');
        $position = $this->request->param('position');
        $data = \think\Db::name('mental_invitationcode')->where(['invitation_code'=>$invitation_code])->find();
        if (!$data) {
            
            
            $this->error('邀请码不存在');
        }
        $num = $data['num'] - $data['used_num'];
        if ($num<=0) {
            $this->error('邀请码使用次数已用完');
        }
         $uid = $this->auth->id??0;
        
        $testlog = Testlog::create(['user_id' => $uid, 'main_id' => $data['main_id'],'invitation_code'=>$invitation_code,'full_name'=>$full_name,'phone'=>$phone,'position'=>$position,]);
        if ($testlog) {
            // var_dump($testlog->id);die;
            \addons\mental\model\TestMainStatistics::setIncNum($data['main_id'], 2);
            $this->success(__('Operation completed'), ['test_id' => $testlog->id,'main_id'=>$data['main_id']]);
        }
        $this->error(__('Operation failed'));
        
        
    }
    
    /**
     * 赠他测信息查询(/addons/mental/test/zengtace)
     * @param string $invitation_code 邀请码
     */
    public function zengtace()
    {
        $invitation_code = $this->request->param('invitation_code');
        
        $order = \think\Db::name('mental_order')->where(['type'=>4,'invitation_code'=>$invitation_code])->find();
        if ($order) {
            if ($order['status'] == 'paid') {
                $this->success(__('Operation completed'), ['test_id' => $order['test_id'],'main_id'=>$order['main_id']]);
            }else {
                $this->error('订单未支付');
            }
        }
        $this->error('邀请码不存在');
    }
    
    
    
    /**
     * 是否提示登陆弹框(/addons/mental/test/isLogin)
     * true=谭,false=不弹
     */
    public function isLogin(){
        return config('site.isLogin');
        
    }
    
    
    /**
     * 是否公众号弹框(/addons/mental/test/isofficial)
     * true=谭,false=不弹
     */
    public function isofficial(){
        return config('site.gongzhonghao');
        
    }
    
    
    /**
     * 继续答题弹框(/addons/mental/test/comtinue)
     */
    public function comtinue()
    {
        // var_dump(config('site.comtinue'));die;
        return config('site.comtinue');
    }
    
    
    /**
     * 团测图片(/addons/mental/test/teamimage)
     */
    public function teamimage(){
        $this->success('成功',config('site.teamimage'));
    }

    /**
     * Undocumented function
     * 关键词搜索标题
     * @return void
     */
    public function seachtitle()
    {
        $keyword = $this->request->param('keyword');
        if (!$keyword) {
            $this->error(__('Invalid parameters'));
        }
        $testMain = new TestMain();
        $data = $testMain->field('id,title')->where('title', 'like', "%$keyword%")->where('switch',1)->select();
        if (empty($data)) {
            $this->error(__('No results were found'));
        }
        $this->success(__('Operation completed'), $data);
    }

    /**
     * Undocumented function
     * 热门【15】
     * @return void
     */
    public function hottest()
    {
        $testMain = new TestMain();
        $data = $testMain->field('id,title,description,price,image,test_num')->order('test_num desc')->where('switch',1)->limit(15)->select();
        if (empty($data)) {
            $this->error(__('No results were found'));
        }
        $this->success(__('Operation completed'), $data);
    }

    /**
     * Undocumented function
     * 列表
     * @return void
     */
    public function getlist()
    {
        
        // var_dump($this->auth->id);die;
        $param = $this->request->param();
        $testMain = new TestMain();
        $list = $testMain->tableList($param,$this->auth->id);
        if (!$list->total()) {
            $this->error(__('No results were found'));
        }
        $this->success(__('Operation completed'), $list);
    }

    /**
     * Undocumented function
     * 详情
     * @return void
     */
    public function detail()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            $this->error(sprintf(__("Parameter %s can not be empty"), 'id'));
        }
        $testMain = new TestMain();
        $detail = $testMain->detail($id);
        if (empty($detail)) {
            $this->error(__('No results were found'));
        }
        $this->success(__('Operation completed'), $detail);
    }

    /**
     * Undocumented function
     * 开始测试
     * @return void
     */
    public function start()
    {
        
        $han = new Cache;
        
        $test_id = $this->request->param('test_id');
        if (empty($test_id)) {
            $this->error(sprintf(__("Parameter %s can not be empty"), 'test_id'));
        }
        
        
        
        
        $testLog = new Testlog();
        $log = $testLog
        ->where('id', '=', $test_id)
         ->where('user_id', '=', $this->auth->id)
        ->find();
        if (empty($log)) {
            $this->error(__('No results were found'));
        }
        if ($log->status == 'finish') {
            $this->error(__('This test has been completed'));
        }
        
//        $redisKey = 'testMain'.$test_id;
//        $getArr = $han->get($redisKey);
//        if ($getArr) {
//            $this->success(__('Operation completed'),json_decode($getArr,true));
//        }
        
        $testMain = new TestMain();
        $test = $testMain->testDetail($log->main_id);
        
        
//        $han->set($redisKey,json_encode($test,true));
        $this->success(__('Operation completed'), $test);
    }
}
